package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.PushManagement;
import com.example.prison.service.PushManagementService;
import com.example.prison.mapper.PushManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【push_management】的数据库操作Service实现
* @createDate 2023-05-29 16:43:34
*/
@Service
public class PushManagementServiceImpl extends ServiceImpl<PushManagementMapper, PushManagement>
    implements PushManagementService{

    @Autowired
    private PushManagementMapper pushManagementMapper;

    @Override
    public PageInfo<PushManagement> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(pushManagementMapper.get());
    }

    @Override
    public int add(PushManagement push) {
        String alarmLevel = push.getAlarmLevel();
        if (ObjectUtils.isNotEmpty(pushManagementMapper.getByAlarmLevel(alarmLevel))) {
            return -1;
        } else {
            pushManagementMapper.add(push);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        pushManagementMapper.deletePush(ids);
    }

    @Override
    public int update(PushManagement push, Integer id) {
        PushManagement pushDTO = pushManagementMapper.getById(id);
        pushManagementMapper.updatePush(push, id);
        List<PushManagement> pushList = pushManagementMapper.getByAlarmId(pushDTO.getId());
        if(pushList.size() > 1) {
            pushManagementMapper.updatePushFail(pushDTO, id);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public PushManagement getById(Integer id) {
        return pushManagementMapper.getById(id);
    }

    @Override
    public PageInfo<PushManagement> getPushByAlarmLevel(int pageNumber, int pageSize, String alarmLevel) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(pushManagementMapper.getPushByAlarmLevel(alarmLevel));
    }

    @Override
    public PushManagement getOneByAlarmLevel(String alarmLevel) {
        return pushManagementMapper.getOneByAlarmLevel(alarmLevel);
    }


}




